package xkp.lesson3;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Scanner;

public class Client {
    public static void main(String[] args) throws UnknownHostException, IOException {
        //Socket socket = new Socket("192.168.92.147", 8888);
        Socket socket = new Socket("127.0.0.1", 8888);
        Scanner scanner = new Scanner(System.in);
        DataOutputStream dataOutputStream = new DataOutputStream(socket.getOutputStream());
        new Thread() {
            @Override
            public void run() {
                DataInputStream dataInputStream = null;
                try {
                    dataInputStream = new DataInputStream(socket.getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                while(true) {
                    try {
                        byte[] bufs = new byte[1024];
                        int len = dataInputStream.read(bufs);
                        System.out.println(new String(bufs,0,len));
                    } catch (IOException e) {
                        System.out.println("连接服务器失败");
                        System.exit(1);
                    }
                }
            };
        }.start();
        while(true) {
            String msg = scanner.nextLine();
            dataOutputStream.write(msg.getBytes());;
            dataOutputStream.flush();
        }
    }
}
